/// <reference path="./BaseOral.ts" />
namespace wmedu {
    export class OralAddSub3 extends BaseOral implements IOralCreater {

        create(opt: IOralOpt) {
            let paramArea = opt.paramArea;
            let ops: Enum_OralOp[] = [];
            // 先得到结果值
            let value = this.mGetRandomInt(paramArea);
            // 再通过随机等号左边第一位数字，大于结果，减，小于结果，只能加
            let p1 = this.mGetRandomInt(paramArea);
            let p2 = this.mGetRandomInt(paramArea);
            let p3 = 0;

            if (Math.random() > 0.5) {
                // 先加再减
                let m = p1 + p2;
                ops.push(Enum_OralOp.Add)
                if (m > value) {
                    p3 = m - value
                    ops.push(Enum_OralOp.Sub)
                } else {
                    if (m == value) {
                        if (p1 < p2) {
                            let t = p1;
                            p1 = p2;
                            p2 = t;
                        }
                        p2 = MathUtil.randomInt(p1, 1);
                        m = p1 + p2;
                    }
                    p3 = value - m
                    ops.push(Enum_OralOp.Add)
                }
            } else {
                // 先减再加
                if (p1 < p2) {
                    let t = p1;
                    p1 = p2;
                    p2 = t;
                }
                let m = p1 - p2;
                ops.push(Enum_OralOp.Sub)
                if (m > value) {
                    p3 = m - value
                    ops.push(Enum_OralOp.Sub)
                } else {
                    if (m == value) {
                        p2 = MathUtil.randomInt(p1, 1);
                        m = p1 + p2;
                    }
                    p3 = value - m
                    ops.push(Enum_OralOp.Add)
                }
            }

            let idx = MathUtil.randomFloor(3);
            let str = ''
            if (opt.blankSide == 0) {
                if (idx == 0) {
                    str = `${opt.blankStr}${this.mGetOpStr(ops[0])}${p2}${this.mGetOpStr(ops[1])}${p3}=${value}`
                } else if (idx == 1) {
                    str = `${p1}${this.mGetOpStr(ops[0])}${opt.blankStr}${this.mGetOpStr(ops[1])}${p3}=${value}`
                } else if (idx == 2) {
                    str = `${p1}${this.mGetOpStr(ops[0])}${p2}${this.mGetOpStr(ops[1])}${opt.blankStr}=${value}`
                }
            } else if (opt.blankSide == 1) {
                str = `${p1}${this.mGetOpStr(ops[0])}${p2}${this.mGetOpStr(ops[1])}${p3}=${opt.blankStr}`
            }

            let res: IOralSide = { nums: [p1, p2, p3], ops: ops, value: value, str }
            return res;
        }
    }
}